package com.ttt.adbgui.utils;

import com.ttt.adbgui.jcomp.JFrameComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Objects;

@Slf4j
@Component
public class JavaCMD {

    private ArrayList<String> strings;
    private Process adb = null;

    @Autowired
    private JFrameComponent jFrameComponent;


    // 使用GBK
    public ArrayList<String> exec(String cmd, JTextPane textPane){
        try {
            adb = Runtime.getRuntime().exec(cmd);
        }catch (IOException e){
            e.printStackTrace();
        }

        InputStream inStream = adb.getInputStream();
        // 设置编码 使在shell中执行返回的数据可以直接读取中文，不会乱码
        InputStreamReader streamReader = new InputStreamReader(inStream, Charset.forName("GBK"));
        BufferedReader buffer = new BufferedReader(streamReader);

        try {
            strings = new ArrayList<>();
            String content = buffer.readLine();
            while (content != null) {
                strings.add(" " + content);
                String text = paramString(strings);
                textPane.setText(text);
                content = buffer.readLine();
            }
        }catch (IOException e){
            e.printStackTrace();
        }

        return strings;
    }

    public ArrayList<String> execDefault(String cmd){
        log.info(cmd);
        try {
            adb = Runtime.getRuntime().exec(cmd);
        }catch (IOException e){
            e.printStackTrace();
        }

        InputStream inStream = adb.getInputStream();
        // 在shell中执行返回的数据可以直接读取中文，不会乱码
        InputStreamReader streamReader = new InputStreamReader(inStream);
        BufferedReader buffer = new BufferedReader(streamReader);

        try {
            strings = new ArrayList<>();
            String content = buffer.readLine();
            while (content != null) {
                if (!content.equals("") && Objects.nonNull(content)){
                    strings.add(" " + content);
                }
                content = buffer.readLine();
            }
        }catch (IOException e){
            e.printStackTrace();
        }

        log.info("cmd result：{}",strings);
        return strings;
    }

    // 执行文件操作
    public void execSubmit(String cmd){
        try {
            adb = Runtime.getRuntime().exec(cmd);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        JavaCMD javaCMD = new JavaCMD();
        // javaCMD.exec("ipconfig");
    }

    public String paramString(ArrayList<String> arrayList){
        String text = new String();
        for (int i = 0; i < arrayList.size(); i++) {
            text = text + arrayList.get(i) + "\n";
        }
        return text;
    }

    // execAppend
    public ArrayList<String> execAppend(String cmd, JTextArea jTextArea){

        cmd = "adb -s" + jFrameComponent.getJLabelSelect().getText() + " " + cmd;
        try {
            adb = Runtime.getRuntime().exec(cmd);
            log.info(cmd);
        }catch (IOException e){
            e.printStackTrace();
        }

        InputStream inStream = adb.getInputStream();
        // 设置编码 使在shell中执行返回的数据可以直接读取中文，不会乱码
        InputStreamReader streamReader = new InputStreamReader(inStream, Charset.forName("GBK"));
        BufferedReader buffer = new BufferedReader(streamReader);

        try {
            strings = new ArrayList<>();
            String content = buffer.readLine();
            while (content != null) {
                jTextArea.append(content+"\n");
                content = buffer.readLine();
            }
        }catch (IOException e){
            e.printStackTrace();
        }

        return strings;
    }
}
